import { request } from '../request';

/** get role list */
export function fetchGetRoleList(params?: Api.SystemManage.RoleSearchParams) {
  return request<Api.SystemManage.RoleList>({
    url: '/sys/role/list',
    method: 'post',
    data: params
  });
}

/**
 * get all roles
 *
 * these roles are all enabled
 */
export function fetchGetAllRoles() {
  return request<Api.SystemManage.AllRole[]>({
    url: '/sys/role/all',
    method: 'get'
  });
}

/** add role */
export function fetchAddRole(data: Api.SystemManage.Role) {
  return request({
    url: '/sys/role/add',
    method: 'post',
    data
  });
}

/** update role */
export function fetchUpdateRole(data: Api.SystemManage.Role) {
  return request({
    url: '/sys/role/update',
    method: 'post',
    data
  });
}

/** update role menu permissions */
export function fetchUpdateRoleMenuPermissions(roleId: number, menuIds: number[]) {
  return request({
    url: '/sys/role/update/menu',
    method: 'post',
    data: {
      roleId,
      menuIds
    }
  });
}

/** get role menu permissions */
export function fetchGetRoleMenuPermissions(roleId: number) {
  return request<number[]>({
    url: `/sys/role/menu-permissions/${roleId}`,
    method: 'get'
  });
}

/** delete role */
export function fetchDeleteRole(id: number) {
  return request({
    url: `/sys/role/delete/${id}`,
    method: 'delete'
  });
}

/** batch delete roles */
export function fetchBatchDeleteRole(ids: number[]) {
  return request({
    url: '/sys/role/batch-delete',
    method: 'delete',
    data: { ids }
  });
}

/** get user list */
export function fetchGetUserList(params?: Api.SystemManage.UserSearchParams) {
  return request<Api.SystemManage.UserList>({
    url: '/sys/user/list',
    method: 'post',
    data: params
  });
}

/** add user */
export function fetchAddUser(data: Api.SystemManage.User) {
  return request({
    url: '/sys/user/add',
    method: 'post',
    data
  });
}

/** update user */
export function fetchUpdateUser(data: Api.SystemManage.User) {
  return request({
    url: '/sys/user/update',
    method: 'put',
    data
  });
}

/** delete user */
export function fetchDeleteUser(id: number) {
  return request({
    url: `/sys/user/delete/${id}`,
    method: 'delete'
  });
}

/** batch delete users */
export function fetchBatchDeleteUser(ids: number[]) {
  return request({
    url: '/sys/user/batch-delete',
    method: 'delete',
    data: { ids }
  });
}

/** get menu list */
export function fetchGetMenuList() {
  return request<Api.SystemManage.MenuList>({
    url: '/sys/menu/list',
    method: 'get'
  });
}

/** get all pages */
export function fetchGetAllPages() {
  return request<string[]>({
    url: '/sys/menu/all-pages',
    method: 'get'
  });
}

/** get menu tree */
export function fetchGetMenuTree() {
  return request<Api.SystemManage.MenuTree[]>({
    url: '/sys/menu/tree',
    method: 'get'
  });
}

/** add menu */
export function fetchAddMenu(data: Api.SystemManage.Menu) {
  return request({
    url: '/sys/menu/add',
    method: 'post',
    data
  });
}

/** update menu */
export function fetchUpdateMenu(data: Api.SystemManage.Menu) {
  return request({
    url: '/sys/menu/update',
    method: 'post',
    data
  });
}

/** delete menu */
export function fetchDeleteMenu(id: number) {
  return request({
    url: '/sys/menu/delete',
    method: 'post',
    data: { id }
  });
}

/** batch delete menu */
export function fetchBatchDeleteMenu(ids: number[]) {
  return request({
    url: '/sys/menu/batch-delete',
    method: 'post',
    data: { ids }
  });
}

/** update menu order */
export function fetchUpdateMenuOrder(data: { id: number; orderNum: number }[]) {
  return request({
    url: '/sys/menu/update-order',
    method: 'post',
    data: { items: data }
  });
}

/** get log list */
export function fetchGetLogList(params?: Api.SystemManage.LogSearchParams) {
  return request<Api.SystemManage.LogList>({
    url: '/sys/log/list',
    method: 'post',
    data: params
  });
}

/** get dept list */
export function fetchGetDeptList(params?: Api.SystemManage.DeptSearchParams) {
  return request<Api.SystemManage.DeptList>({
    url: '/sys/dept/list',
    method: 'post',
    data: params
  });
}

/** get dept tree */
export function fetchGetDeptTree() {
  return request<Api.SystemManage.DeptTree[]>({
    url: '/sys/dept/tree',
    method: 'get'
  });
}

/** get position list */
export function fetchGetPositionList(params?: Api.SystemManage.PostSearchParams) {
  return request<Api.SystemManage.PostList>({
    url: '/sys/post/list',
    method: 'post',
    data: params
  });
}

/** get all post list */
export function fetchGetPostList() {
  return request<Api.SystemManage.Post[]>({
    url: '/sys/post/all',
    method: 'get'
  });
}

/** add position */
export function fetchAddPosition(data: Api.SystemManage.Post) {
  return request({
    url: '/sys/post/add',
    method: 'post',
    data
  });
}

/** update position */
export function fetchUpdatePosition(data: Api.SystemManage.Post) {
  return request({
    url: '/sys/post/update',
    method: 'post',
    data
  });
}

/** delete position */
export function fetchDeletePosition(id: number) {
  return request({
    url: `/sys/post/delete/${id}`,
    method: 'delete'
  });
}

/** batch delete positions */
export function fetchBatchDeletePosition(ids: number[]) {
  return request({
    url: '/sys/post/batch-delete',
    method: 'delete',
    data: { ids }
  });
}

/** add dept */
export function fetchAddDept(data: Api.SystemManage.Dept) {
  return request({
    url: '/sys/dept/add',
    method: 'post',
    data
  });
}

/** update dept */
export function fetchUpdateDept(data: Api.SystemManage.Dept) {
  return request({
    url: '/sys/dept/update',
    method: 'post',
    data
  });
}

/** delete dept */
export function fetchDeleteDept(id: number) {
  return request({
    url: `/sys/dept/delete/${id}`,
    method: 'delete'
  });
}

/** batch delete depts */
export function fetchBatchDeleteDept(ids: number[]) {
  return request({
    url: '/sys/dept/batch-delete',
    method: 'delete',
    data: { ids }
  });
}
